﻿namespace MicroCloud.Api.Systems.Controllers
{
    /// <summary>
    /// 文件处理
    /// </summary>
    [Description("文件处理")]
    public class FileController : SystemsControllerBase
    {
        /// 获取 文件服务
        protected IFileService FileService => ServiceProvider.GetService<IFileService>();

        /// <summary>
        /// 获取映射到静态资源的相对请求路径
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Description("静态资源请求路径")]
        public virtual string RequestPath()
        {
            var requestPath = FileService.GetRequestPath();
            return requestPath;
        }

        /// <summary>
        /// 计算文件的Base64字符串
        /// </summary>
        /// <param name="file">请求中的文件信息</param>
        /// <returns></returns>
        [HttpPost]
        [DisableRequestSizeLimit]
        [Description("计算文件的Base64字符串")]
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual string GetFileBase64String([FromForm(Name = "file")] IFormFile file)
        {
            //将文件转为流
            using var stream = file.OpenReadStream();
            string base64String = null;

            if (stream == null)
            {
                return null;
            }
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            base64String = Convert.ToBase64String(bytes);
            stream.Close();

            return base64String;
        }

        /// <summary>
        /// 获取文件信息
        /// </summary>
        /// <param name="id">文件ID</param>
        /// <returns></returns>
        [HttpGet]
        [DisableRequestSizeLimit]
        [Description("文件信息")]
        public virtual async Task<AjaxResult> Get(string id)
        {
            var outputDto = await FileService.GetFileInfo(id);
            return AjaxResult.Success(outputDto);
        }

        /// <summary>
        /// 缓存式文件上传
        /// </summary>
        /// <param name="file">请求中的文件信息</param>
        [HttpPost]
        [UnitOfWork]
        [DisableRequestSizeLimit]
        [Description("缓存式文件上传")]
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<AjaxResult> Upload([FromForm(Name = "file")] IFormFile file)
        {
            using var stream = file.OpenReadStream();
            var uploadFileDto = new UploadInputDto
            {
                FileStream = stream,
                FileName = Path.GetFileNameWithoutExtension(file.FileName),//文件名，不含扩展名
                FileExtension = Path.GetExtension(file.FileName)//文件扩展名，包含“.”
            };

            var result = await FileService.UploadAsync(uploadFileDto); 
            return result.ToAjaxResult();
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="id">文件ID</param>
        /// <returns></returns>
        [HttpGet]
        [DisableRequestSizeLimit]
        [Description("下载文件")]
        public virtual async Task<IActionResult> Download(string id)
        {
            var result = await FileService.DownloadAsync(id);
            if (!result.Succeeded) return NoContent();

            var downloadDto = result.Data;
            return File(downloadDto.FileStream
                , MediaTypeNames.Application.Octet
                , WebUtility.HtmlEncode(downloadDto.FileFullName));
        }

        /// <summary>
        /// 缓存式文件更新
        /// </summary>
        /// <param name="file">请求中的文件信息</param>
        /// <param name="id">文件ID</param>
        /// <returns></returns>
        [HttpPost]
        [UnitOfWork]
        [DisableRequestSizeLimit]
        [Description("缓存式文件更新")]
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<AjaxResult> Update([FromForm(Name = "file")] IFormFile file, [FromForm(Name = "id")] string id)
        {
            using var stream = file.OpenReadStream();
            var updateFileDto = new UploadInputDto
            {
                Id = id,
                FileName = Path.GetFileNameWithoutExtension(file.FileName),//文件名，不含扩展名
                FileExtension = Path.GetExtension(file.FileName),//文件扩展名，包含“.”
                FileStream = stream,
            };
            var result = await FileService.UpdateAsync(updateFileDto);
            return result.ToAjaxResult();
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="id">文件ID</param>
        /// <returns></returns>        
        [HttpGet]
        [UnitOfWork]
        [Description("删除文件")]
        public virtual async Task<AjaxResult> Delete(string id)
        {
            var result = await FileService.DeleteAsync(id);
            return result.ToAjaxResult(true);
        }

    }
}